package com.sskj.bibi.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hey.lib.HeySpinner;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.header.MaterialHeader;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.bean.CoinTypeBean;
import com.sskj.bibi.bean.EntrustBean;
import com.sskj.bibi.presenter.HistoryEntrustActivityPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.BIBI_HISTORY_ENTRUST)
public class HistoryEntrustActivity extends BaseActivity<HistoryEntrustActivityPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvHistory)
    TextView tvHistory;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R2.id.leftSpinner)
    HeySpinner leftSpinner;
    @BindView(R2.id.rightSpinner)
    HeySpinner rightSpinner;
    @BindView(R2.id.bottomSpinner)
    HeySpinner bottomSpinner;
    @BindView(R2.id.btConfirm)
    Button btConfirm;
    @BindView(R2.id.filter_layout_root)
    LinearLayout filterLayoutRoot;
    @BindView(R2.id.view)
    View view;
    @BindView(R2.id.btCancel)
    Button btCancel;
    private SlimAdapter slimAdapter;
    String code;
    String type = null;
    String status = "4";
    @Autowired
    String qu;
    private List<CoinTypeBean> coinTypeBeans;
    private HashMap<Integer, String> statusMap;
    private HashMap<Integer, String> typeMap;
    private MVCHelper mvcHelper;
    private HashMap<String, String> stateMap;
    private List<String> leftCodes;

    @Override
    protected int getLayoutId() {
        return R.layout.bibi_activity_history_entrust;
    }

    @Override
    public HistoryEntrustActivityPresenter getPresenter() {
        return new HistoryEntrustActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        stateMap = new HashMap<>();
        stateMap.put("0", App.INSTANCE.getString(R.string.bibi_historyEntrustActivity1));
        stateMap.put("1", App.INSTANCE.getString(R.string.bibi_historyEntrustActivity2));
        stateMap.put("2", App.INSTANCE.getString(R.string.bibi_historyEntrustActivity3));
        stateMap.put("-1", App.INSTANCE.getString(R.string.bibi_historyEntrustActivity4));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 29))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setPaintColor(ContextCompat.getColor(App.INSTANCE, R.color.bibi_divider))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.bibi_recy_item_entrust_history, new SlimInjector<EntrustBean>() {
            @Override
            public void onInject(EntrustBean data, IViewInjector injector, List<Object> payloads) {
                injector.text(R.id.entrust_type, data.isBuy() ? App.INSTANCE.getString(R.string.bibi_historyEntrustActivity5) : App.INSTANCE.getString(R.string.bibi_historyEntrustActivity6))
                        .textColor(R.id.entrust_type, ContextCompat.getColor(App.INSTANCE, data.getType().equals("1") ? R.color.bibi_03c : R.color.bibiRedText))
                        .text(R.id.entrust_name, data.getL_code() + "/" + data.getR_code())
                        .text(R.id.entrust_time, data.getTrade_time())
                        .text(R.id.entrust_prePrice, data.getOtype().equals("2") ? App.INSTANCE.getString(R.string.bibi_historyEntrustActivity7) : data.getWtprice())
                        .text(R.id.entrust_prePrice_text, String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity8), data.getR_code()))
                        .text(R.id.entrust_totalPrice,data.getTotalprice())
                        .text(R.id.entrust_totalPrice_text, String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity9), data.getR_code()))
                        .text(R.id.entrust_realNum, data.getCjnum())
                        .text(R.id.entrust_realNum_text, String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity10), data.getL_code()))
                        .text(R.id.entrust_realPrice, data.getCjprice())
                        .text(R.id.entrust_realPrice_text, String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity11), data.getR_code()))
                        .text(R.id.entrust_state, stateMap.get(data.getStatus() + ""));
                if (data.isMarketBuy()) {  //市价买入
                    injector.text(R.id.entrust_preNum_text, String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity12), data.getR_code()))
                            .text(R.id.entrust_preNum, data.getTotalprice());
                } else { //其他
                    injector.text(R.id.entrust_preNum_text, String.format(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity13), data.getL_code()))
                            .text(R.id.entrust_preNum, data.getWtnum());
                }
            }
        }).attachTo(recyclerView);
        slimAdapter.updateData(new ArrayList());
        ClickUtil.click(tvHistory, () -> {
            if (filterLayoutRoot.getVisibility() == View.VISIBLE) {
                filterLayoutRoot.setVisibility(View.GONE);
            } else {
                filterLayoutRoot.setVisibility(View.VISIBLE);
                if (rightSpinner != null) {
                    rightSpinner.setSelectIndex(0);
                    leftSpinner.setSelectIndex(0);
                }
            }
        });
        ClickUtil.click(view, () -> {
            filterLayoutRoot.setVisibility(View.GONE);
        });
        ClickUtil.click(btCancel, () -> {
            filterLayoutRoot.setVisibility(View.GONE);
        });

        coolRefreshView.setPullHeader(new MaterialHeader(this));
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource refreshDataSource = new ModelRx2DataSource<EntrustBean>(
                (ModelRx2DataSource.OnLoadSource<EntrustBean>)
                        page -> mPresenter.getEntrustHistoryFlow(code, type, status, page, qu), 10);
        // 设置数据源
        mvcHelper.setDataSource(refreshDataSource);
        // 设置适配器
        mvcHelper.setAdapter(slimAdapter);
        ArrayList<String> typeMap = new ArrayList<>();
        typeMap.add(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity14));
        typeMap.add(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity5));
        typeMap.add(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity6));
        rightSpinner.attachData(typeMap);
        ArrayList<String> statusMap = new ArrayList<>();
        statusMap.add(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity14));
        statusMap.add(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity3));
        statusMap.add(App.INSTANCE.getString(R.string.bibi_historyEntrustActivity4));
        bottomSpinner.attachData(statusMap);
    }

    @Override
    protected void initData() {
        statusMap = new HashMap();
        statusMap.put(0, "4");
        statusMap.put(1, "2");
        statusMap.put(2, "-1");
        typeMap = new HashMap();
        typeMap.put(0, null);
        typeMap.put(1, "1");
        typeMap.put(2, "2");
        mPresenter.getAllCoin(qu);
        mvcHelper.refresh();
    }

    public void updateCoinType(List<CoinTypeBean> data) {
        this.coinTypeBeans = data;
        Flowable.fromIterable(coinTypeBeans)
                .map(coinTypeBean -> coinTypeBean.getMark().replace("_", "/").toUpperCase())
                .distinct()
                .toList()
                .subscribe((strings, throwable) -> {
                    strings.add(0, App.INSTANCE.getString(R.string.bibi_historyEntrustActivity14));
                    leftCodes = strings;
                    leftSpinner.setItemGravity(Gravity.CENTER);
                    leftSpinner.attachData(strings);
                    code = null;
                    leftSpinner.setOnSelectListener(position -> {
                        if (position == 0) {
                            code = null;
                        } else {
                            code = strings.get(position);
                        }
                    });
                });


        ClickUtil.click(btConfirm, () -> {
            filterLayoutRoot.setVisibility(View.GONE);
            int rightPos = leftSpinner.getSelectIndex();
            if (rightPos == 0) {
                code = null;
            } else {
                code = leftCodes.get(leftSpinner.getSelectIndex());
            }
            type = typeMap.get(rightSpinner.getSelectIndex());
            status = statusMap.get(bottomSpinner.getSelectIndex());
            coolRefreshView.setRefreshing(true);
        });
    }

}
